<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/6/26 0026
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;


use app\admin\model\AdminUser;
use app\admin\validate\CheckAdminUser;
use app\common\controller\AdminBaseController;
use app\common\model\personnel\PersonnelOrder;
use app\common\model\personnel\PersonnelStudent;
use app\common\model\personnel\PersonnelType;
use app\common\model\personnel\PersonnelYear;
use app\common\service\GoogleAuthenticator;
use think\Exception;
use think\facade\Db;

class Statistics extends AdminBaseController
{
    protected $orderModel;
    protected $studentModel;
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->orderModel = new PersonnelOrder();
        $this->studentModel = new PersonnelStudent();
    }

    /**
     * @return string
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:index
     */
    public function index(){

        $both_time = $this->request->param('both_time');

        if (IS_AJAX){
            if(empty($both_time)){
                $both_time = date("Y-m-d", time()).' - '.date("Y-m-d", strtotime("+7 day", time()));
            }
            $bothTime = explode(' - ' ,$both_time);
            $start_time = strtotime($bothTime[0]);
            $end_time = strtotime($bothTime[1]);
            $days = round(($end_time-$start_time)/3600/24);
            $data = [];
            for ($i=0; $i<$days+1; $i++){
                $date = date("Y-m-d", strtotime("+{$i} day", $start_time));
                $currentStartTime = date("Y-m-d 00:00:00", strtotime($date));
                $currentEndTime = date("Y-m-d 23:59:59", strtotime($date));
                $result = $this->orderModel->where('create_time','between',[strtotime($currentStartTime),strtotime($currentEndTime)])->select();
                $report = 0;
                $price = 0;
                $refund = 0;
                $refund_price = 0;
                foreach ($result as $key => $value){
                    if($value['pay_status'] == -1){
                        $refund += 1;
                        $refund_price += $value['price'];
                    }else{
                        $report += 1;
                        $price += $value['price'];
                    }
                }
                $data[] = [
                    'id' => $i,
                    'date' => $date,
                    'report' => $report,
                    'price' => round($price/100, 2),
                    'refund' => $refund,
                    'refund_price' => $refund_price,
                    'student' => $this->studentModel->where('create_time','between',[strtotime($currentStartTime),strtotime($currentEndTime)])->count("id")
                ];
            }

            return parent::ajaxResult($data);
        }
        $sTime = date("Y-m-d 00:00:00", time());
        $eTime = date("Y-m-d 23:59:59", time());
        $this->assign([
            'registerStudent' => $this->orderModel->where('create_time','between',[strtotime($sTime),strtotime($eTime)])->count('id'),
            'reportStudent' => $this->studentModel->where('create_time','between',[strtotime($sTime),strtotime($eTime)])->count('id'),
            'price' => round($this->orderModel->where('create_time','between',[strtotime($sTime),strtotime($eTime)])->sum('price')/100, 2),
        ]);
        return $this->fetch();
    }
    /**
     * @return mixed|string|\think\response\Json
     * @throws \think\db\exception\DbException
     * @author: Hhy <jackhhy520@qq.com>
     * @date: 2020/6/30 0030
     * @describe:添加编辑
     */
    public function add()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            //验证提交的数据
            return $this->model->doAll($data);
        }

        $id = $this->request->param("id/d");//父id
        $info = $this->model->find($id);

        $this->assign(compact("info", "id"));
        return $this->fetch();
    }


}